package com.twu.biblioteca;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by yiman on 6/23/14.
 */
public class Command {

    public boolean validCommand(String command){
        return command.equals("BL") || command.equals("R") || command.equals("C") ||
                command.equals("S") || command.equals("CM") || command.equals("L") ||
                command.equals("Quit");
    }

    public String getResponse(String command, InputStream in,PrintStream out){
        if(!validCommand(command)){
            return "Select a valid option!";
        }
        else{
            BibliotecaLibrary library = new BibliotecaLibrary();
            Scanner keyboard = new Scanner((in));
            if (command.equals("BL")){
                HashMap<String,Book> bookLibrary = library.showAllBooksDetails();
                Collection<Book> bookCollection = bookLibrary.values();
                for (Book book : bookCollection) {
                    out.println("Book Name: " + book.getBookName());
                    out.println("Author: " + book.getAuthor());
                    out.println("Year Published: " + book.getYearPublished());
                    out.println("---------------------------------------------");
                }
            }
            if (command.equals("C")){
                Book book = new Book();
                out.println("Please enter the book name: ");
                book.setBookName(keyboard.nextLine());
                out.println("Please enter the author name: ");
                book.setAuthor(keyboard.nextLine());
                out.println("Please enter the year published: ");
                book.setYearPublished(Integer.parseInt(keyboard.nextLine()));
                out.println(library.checkOutBook(book));
            }
            if (command.equals("R")){
                Book book = new Book();
                out.println("Please enter the book name: ");
                book.setBookName(keyboard.nextLine());
                out.println("Please enter the author name: ");
                book.setAuthor(keyboard.nextLine());
                out.println("Please enter the year published: ");
                book.setYearPublished(Integer.parseInt(keyboard.nextLine()));
                out.println(library.returnBook(book));
            }
            if(command.equals("CM")){
                Movie movie = new Movie();
                out.println("Please enter the movie name: ");
                movie.setName(keyboard.nextLine());
                out.println("Please enter the year: ");
                movie.setYear(Integer.parseInt(keyboard.nextLine()));
                out.println("Please enter the Director: ");
                movie.setDirector(keyboard.nextLine());
                out.println("Please enter the rate: ");
                movie.setRate(keyboard.nextLine());
                out.println(library.checkOutMovie(movie));
            }
            if(command.equals("L")){
                out.println("Please enter your username: ");
                String username = keyboard.nextLine();
                out.println("Please enter your password");
                String password = keyboard.nextLine();

                if (library.userLogin(username,password)){
                    out.println("Congratulations! You can view your profile by comand (S)");
                }
                else{
                    out.println("Wrong Username or Passport");
                }
            }
            if(command.equals("S")){
                User currentUser = library.getCurrentUser();
                if(currentUser != null) {
                    out.println(currentUser.getName());
                    out.println(currentUser.getEmailAddress());
                    out.println(currentUser.getPhoneNumber());
                }
                else
                    out.println("You have to login first!");
            }
            if(command.equals("Quit")){
                return "quit";
            }
        }
        return "success";
    }

}
